package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyLikeMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 攻略的资源过滤器
 * */
@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyDetailService detailService;
    @Autowired
    private IStrategyCommentService commentService;
    @Autowired
    private StrategyCommentMapper commentMapper;
    @Autowired
    private StrategyLikeMapper likeMapper;
    @Autowired
    private ITagService tagService;

    @GetMapping
    public PageInfo queryTravel(StrategyCatalogQueryObject qo){
        PageInfo<Strategy> pageInfo = strategyService.query(qo);
        return pageInfo;
    }

    @GetMapping("/{strategyId}/catalogs")
    public List<StrategyCatalog> queryStrategiesByRegionId(@PathVariable Long strategyId){
        List<StrategyCatalog> listAll =strategyCatalogService.queryForStrategyId(strategyId);
        return  listAll;
    }

    /**
     * 根据攻略的id查询该攻略的详细信息
     * @param id 攻略的id
     * @return 攻略文章对象
     */
    @GetMapping("/details/{id}")
    public StrategyDetail getByDetailsId(@PathVariable Long id){
        return detailService.getByDetailsId(id);
    }

    @GetMapping("/{strategyId}")
    public Strategy getByStrategyId(@PathVariable Long strategyId){
        return strategyService.get(strategyId);
    }

    /**
     * 保存大攻略下的一级评论
     * @param strategyComment 攻略评论的对象
     * @param tags 给评论贴得标签数组
     * @return JsonResult对象
     */
    @PostMapping("/{strategy.id}/comments")
    public JsonResult saveComment(StrategyComment strategyComment,String[] tags){
        commentService.save(strategyComment,tags);
        return new JsonResult();
    }

    /**
     * 保存大攻略下的一级评论
     * @param strategyComment  攻略评论的对象
     * @return JsonResult对象
     */
    @PostMapping("/{parent.id}/replyComment")
    public JsonResult saveReplyComment(StrategyComment strategyComment){
        commentService.saveReplyComment(strategyComment);
        return new JsonResult();
    }


    /**
     * 获取大攻略下的一级评论的集合
     * @param qo
     * @return
     */
    @GetMapping("/{strategyId}/comments")
    public PageInfo<StrategyComment> getComments(StrategyCatalogQueryObject qo){
        qo.setOrderBy("st.createTime desc");
        PageInfo<StrategyComment> parentComments = commentService.queryCommentsByStrategyId(qo);
        for (StrategyComment ele:parentComments.getList()){
            StrategyCatalogQueryObject childer = new StrategyCatalogQueryObject();
            childer.setParentId(ele.getId());
            int likeNum = likeMapper.selectByCommentIdLike(ele.getId());
            ele.setLikeNum(likeNum);
            List<StrategyComment> childerQuery = commentMapper.queryCommentsByStrategyId(childer);
            ele.setChilderList(childerQuery);
            ele.setCommentNums(childerQuery.size());
        }
        return parentComments;
    }

    /**
     * 根据大攻略的id查询用户最常用的6个标签
     * @param strategyId 大攻略的id
     * @return 标签List集合
     */
    @GetMapping("/{strategyId}/tags")
    public List getTags(@PathVariable Long strategyId){
        List<Tag> list = tagService.listForTags(strategyId);
        return list;
    }
}
